fix: add Vitest fake timer detection to asyncWrapper - #1443
Conversation
The asyncWrapper's setTimeout(0) drain mechanism requires advancing fake timers when they are active. Previously, only Jest fake timers were detected (via `jest` global), causing Vitest users with fake timers to experience hangs in `waitFor` and `user.click` (see testing-library#1197, testing-library#1187). This adds a `vitestFakeTimersAreEnabled()` function that checks for the `vi` global (Vitest's equivalent of `jest`) combined with the `setTimeout.clock` property set by @sinonjs/fake-timers. When Vitest fake timers are detected, `vi.advanceTimersByTime(0)` is called to unblock the drain promise. The detection is extracted into a unified `advanceFakeTimers()` helper that handles both Jest and Vitest paths, keeping the asyncWrapper itself clean. Closes testing-library#1197 Relates to testing-library#1187
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 8a926ae:
|
|
I encountered this leak during development. Since the source of the leak is within this specific library, I would like to have it fixed so that more people can benefit from the solution. cc @eps1lon @MichaelDeBoey |
Ziiyodullayevv
left a comment
There was a problem hiding this comment.
Important fix for Vitest compatibility. Adding fake timer detection to asyncWrapper ensures proper async handling in Vitest test environments.
| // Unlike Jest, Vitest does not set `setTimeout._isMockFunction`. | ||
| return ( | ||
| // eslint-disable-next-line no-undef -- `vi` is a global in Vitest, like `jest` in Jest. | ||
| typeof vi !== 'undefined' && |
There was a problem hiding this comment.
Isn't vi non-existent by default? https://v1.vitest.dev/config/#globals
snowystinger
left a comment
There was a problem hiding this comment.
It'd be really nice if we could expose some sort of property so that we don't need to do fake timer detection in other libraries. For instance, it'd be nice in user event to auto advance a sequence of timers for a series of user actions when fake timers are in use.
testing-library/user-event#1324
From what I can tell right now, we'd be doing a similar if (jest/vi/setTimeout.clock.tick) which could diverge from the tests here
What: Add Vitest fake timer detection to the
asyncWrapperinpure.js, so thatwaitFor(and by extensionuser.clickvia@testing-library/user-event) no longer hangs when Vitest fake timers are active.Why: The
asyncWrapperusessetTimeout(0)to drain the microtask queue after a callback completes. When fake timers are active, thissetTimeoutnever fires unless the test framework advances timers. The existing code only checks for Jest fake timers (via thejestglobal), so Vitest users withvi.useFakeTimers()experience indefinite hangs inwaitForanduser.click. This is the root cause of #1197 and relates to #1187.How:
vitestFakeTimersAreEnabled()— checks for theviglobal ANDsetTimeout.clock(set by@sinonjs/fake-timers, which Vitest uses internally). Both conditions must be true to avoid false positives when Vitest is present but real timers are in use.advanceFakeTimers()— a unified helper that tries Jest first, then Vitest, keeping theasyncWrapperitself clean.asyncWrapper'ssetTimeout(0)+ advance pattern is unchanged; only the detection scope is widened.setTimeout.clockand_isMockFunctionpresence).Note: The Vitest code path cannot be fully integration-tested within Jest (since
jestis always available as a global and takes priority in detection). The detection logic is verified via unit tests that assert the signals each framework sets onsetTimeout. Full Vitest integration was verified manually in a separate Vitest project.Checklist:
Closes #1197
Relates to #1187